/* callbacks.c -- callbacks used by the gui
 *
 * author: Diogo F. S. Ramos <diogofsr@gmail.com>
 */

#include <glade/glade.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "engcur.h"
#include "callbacks.h"
#include "interfaces.h"
#include "support.h"

/* Callbacks *********************************************************/
void
std_dialog_handler( GtkDialog *dialog, gint arg1, gpointer *data )
{
    gint handler = (gint) g_object_get_data( G_OBJECT( dialog ), "handler" );

    switch( handler )
    {
    case DIALOG_ABOUT:
    {
	switch( arg1 )
	{
	case GTK_RESPONSE_CANCEL:
	{
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	
	    break;
	}
	}
	break;
    }
    case DIALOG_NEW:
    {
	switch( arg1 )
	{
	case GTK_RESPONSE_OK:
	{
	    GtkTreeModel *store_disciplines = NULL;
	    GtkTreeModel *store_schedule = NULL;
	    semester *sem = g_object_get_data( G_OBJECT( dialog ),
					       "semester" );

	    store_disciplines = g_object_get_data( G_OBJECT( dialog ),
						   "store_disciplines" );
	    store_schedule = g_object_get_data( G_OBJECT( dialog ),
						"store_schedule" );
	    
	    if( !sem )
		return;

	    semester_clean( sem );

	    tree_model_disciplines_update( store_disciplines, sem );
	    tree_model_schedule_update( store_schedule, sem );

	    gtk_widget_hide( GTK_WIDGET( dialog ) );	
	    
	    break;
	}
	case GTK_RESPONSE_CANCEL:
	{
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	
	    break;
	}
	}	
	break;
    }
    case DIALOG_FILECHOOSER_SAVE:
    {
	switch( arg1 )
	{
	case GTK_RESPONSE_OK:
	{
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	

	    char **filename = g_object_get_data( G_OBJECT( dialog ),
					     "filename" );
	    semester *sem = g_object_get_data( G_OBJECT( dialog ),
					       "semester" );
	    if( !sem )
		return;

	    if( *filename )
		free(*filename);
	    
	    *filename = (char*) gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
	    
	    semester_save_to_file( sem, *filename );
	    
	    break;
	}
	case GTK_RESPONSE_CANCEL:
	{
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	
	    break;
	}
	}	
	break;
    }
    case DIALOG_FILECHOOSER_LOAD:
    {
	switch( arg1 )
	{
	case GTK_RESPONSE_OK:
	{
	    char **filename = g_object_get_data( G_OBJECT( dialog ),
					     "filename" );
	    GtkTreeModel *store_disciplines = NULL;
	    GtkTreeModel *store_schedule = NULL;
	    semester *sem = g_object_get_data( G_OBJECT( dialog ),
					       "semester" );
	    if( sem )
		semester_clean( sem );

	    store_disciplines = g_object_get_data( G_OBJECT( dialog ),
						   "store_disciplines" );
	    store_schedule = g_object_get_data( G_OBJECT( dialog ),
						   "store_schedule" );

	    *filename = (char*) gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );

	    semester_load_from_file( sem, *filename );
	    tree_model_disciplines_update( store_disciplines, sem );
	    tree_model_schedule_update( store_schedule, sem );
	    
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	

	    break;
	}
	case GTK_RESPONSE_CANCEL:
	{
	    gtk_widget_hide( GTK_WIDGET( dialog ) );	
	    break;
	}
	}
	break;
    }
    }
}

void
std_button_handler( GtkButton *button, gpointer data )
{ 
    gint handler = (gint) g_object_get_data( G_OBJECT(button), "handler");
            
    switch( handler )
    {
    case MENUITEM_ABOUT:
    {
	GtkDialog *dialog = NULL;
	dialog = g_object_get_data( G_OBJECT( button ),
				    "dialog_about" );
	gtk_widget_show( GTK_WIDGET( dialog ) );
	break;
    }
    case MENUITEM_NEW:
    {
	GtkDialog *dialog = NULL;
	dialog = g_object_get_data( G_OBJECT( button ),
				    "dialog_new" );
	gtk_widget_show( GTK_WIDGET( dialog ) );
	break;
    }
    case MENUITEM_SAVE_AS:
    {
	GtkDialog *dialog = NULL;
	dialog = g_object_get_data( G_OBJECT( button ),
				    "dialog_filechooser_save" );
	gtk_widget_show( GTK_WIDGET( dialog ) );
	break;
    }
    case MENUITEM_SAVE:
    {
	semester *sem = g_object_get_data( G_OBJECT( button ),
					   "semester" );
	char **filename = g_object_get_data( G_OBJECT( button ),
					    "filename" );

	if( *filename == NULL )
	    return;

	semester_save_to_file( sem, *filename );
	
	break;
    }
    case MENUITEM_LOAD:
    {
	GtkDialog *dialog = NULL;
	dialog = g_object_get_data( G_OBJECT( button ),
				    "dialog_filechooser_load" );
	gtk_widget_show( GTK_WIDGET( dialog ) );
	break;
    }
    case MENUITEM_QUIT:
    {
	gtk_main_quit();
	break;
    }
    case MENUITEM_SCHEDULE_DISC_ADD:
    {
	GtkTreeView *treeview_disciplines = NULL;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;

	treeview_disciplines = GTK_TREE_VIEW(
	    g_object_get_data( G_OBJECT( button ),
			       "treeview_disciplines" ) );
	gtk_tree_view_get_cursor( treeview_disciplines, &path, &column );
	
	if( path != NULL )
	{
	    GtkWidget *store_disciplines = NULL;
	    GtkTreeIter iter;
	    gint depth;
	    
	    store_disciplines = g_object_get_data( G_OBJECT( button ),
				       "store_disciplines" );   
	    gtk_tree_model_get_iter( GTK_TREE_MODEL( store_disciplines ),
				     &iter, path );
	    depth = gtk_tree_store_iter_depth( GTK_TREE_STORE( store_disciplines ),
					       &iter );
	    if( depth == 0 )
	    {
		GtkTreeModel *sortmodel_schedule;
		GtkTreeModel *store_schedule;
		gchar *cod;
		gchar *class;
		int int_class = 0;
		discipline *disc = NULL;
		semester *sem = NULL;

		sortmodel_schedule = g_object_get_data( G_OBJECT( button ),
							"sortmodel_schedule" );
		store_schedule = g_object_get_data( G_OBJECT( button ),
						    "store_schedule" );

		gtk_tree_model_get( GTK_TREE_MODEL(store_disciplines), &iter,
				    DISCIPLINE_COD, &cod,
				    DISCIPLINE_CLASS, &class,
				    -1 );
		sscanf( class, "%d", &int_class );
		sem = g_object_get_data( G_OBJECT( button ),
					 "semester" );
		disc = semester_get_discipline( sem, cod, int_class );

                /* If it was not schedulled before */
		if( semester_schedule_add_discipline( sem, disc ) )
		{
		    tree_model_schedule_update( store_schedule,
						sem );
		    tree_model_disciplines_update( GTK_TREE_MODEL(store_disciplines),
						sem );
		}
	    }
	}
	
	break;
    }
    case MENUITEM_SCHEDULE_DISC_REM:
    {
	GtkTreeView *treeview_disciplines = NULL;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;

	treeview_disciplines = GTK_TREE_VIEW(
	    g_object_get_data( G_OBJECT( button ),
			       "treeview_disciplines" ) );
	gtk_tree_view_get_cursor( treeview_disciplines, &path, &column );
	
	if( path != NULL )
	{
	    GtkWidget *store_disciplines = NULL;
	    GtkTreeIter iter;
	    gint depth;
	    
	    store_disciplines = g_object_get_data( G_OBJECT( button ),
				       "store_disciplines" );   
	    gtk_tree_model_get_iter( GTK_TREE_MODEL( store_disciplines ),
				     &iter, path );
	    depth = gtk_tree_store_iter_depth( GTK_TREE_STORE( store_disciplines ),
					       &iter );
	    if( depth == 0 )
	    {
		GtkTreeModel *sortmodel_schedule;
		GtkTreeModel *store_schedule;
		gchar *cod;
		gchar *class;
		int int_class = 0;
		discipline *disc = NULL;
		semester *sem = NULL;

		sortmodel_schedule = g_object_get_data( G_OBJECT( button ),
							"sortmodel_schedule" );
		store_schedule = g_object_get_data( G_OBJECT( button ),
						    "store_schedule" );

		gtk_tree_model_get( GTK_TREE_MODEL(store_disciplines), &iter,
				    DISCIPLINE_COD, &cod,
				    DISCIPLINE_CLASS, &class,
				    -1 );
		sscanf( class, "%d", &int_class );
		sem = g_object_get_data( G_OBJECT( button ),
					 "semester" );
		disc = semester_get_discipline( sem, cod, int_class );

                /* If it was not schedulled before */
		if( semester_schedule_remove_discipline( sem, disc ) )
		{
		    tree_model_schedule_update( store_schedule,
						sem );
		    tree_model_disciplines_update( GTK_TREE_MODEL(store_disciplines),
						   sem );
		}
	    }
	}
	
	break;
    }
    case MENUITEM_PRINT_TABLE:
    {
	semester *sem = g_object_get_data( G_OBJECT( button ), "semester" );
	semester_print( sem );
	
	break;
    }
    case BUTTON_DISC_ADD:
    {
	GtkTreeStore *store = NULL;
	discipline *disc = NULL;
	GtkWidget *widget = NULL;
	//GtkTreeIter iter;
	const gchar *data[2];
	gint credits;
	gint class;
	//gchar cred[3];
	//gchar clas[3];
	semester *sem = g_object_get_data( G_OBJECT( button ), "semester" );
	
	widget = g_object_get_data( G_OBJECT( button ),
				    "entry_disc_cod" );
	data[0] = gtk_entry_get_text( GTK_ENTRY(widget) );
	widget = g_object_get_data( G_OBJECT( button ),
				    "entry_disc_name" );
	data[1] = gtk_entry_get_text( GTK_ENTRY(widget) );
	widget = g_object_get_data( G_OBJECT( button ),
				    "spinbutton_disc_credits" );
	credits = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(widget) );
	widget = g_object_get_data( G_OBJECT( button ),
				    "spinbutton_disc_class" );
	class = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON(widget) );

	disc = discipline_new( (char*)data[0], (char*)data[1], (int)credits, (int)class );

	if( semester_add_discipline( sem, disc ) == 1 )
	{
	    store = g_object_get_data( G_OBJECT( button ), "store_disciplines" );
	    tree_model_disciplines_update( GTK_TREE_MODEL(store), sem );
	    
	    /*
	    snprintf( cred, 3, "%d", credits );
	    snprintf( clas, 3, "%d", class );
	    store = g_object_get_data( G_OBJECT( button ), "store_disciplines" );
	    gtk_tree_store_append( store, &iter, NULL );
	    gtk_tree_store_set( store, &iter,
				DISCIPLINE_COD, disc->cod,
				DISCIPLINE_CLASS, clas,
				DISCIPLINE_NAME, disc->name,
				DISCIPLINE_CREDITS, cred,
				DISCIPLINE_DAY, "",
				DISCIPLINE_BEGIN, "",
				DISCIPLINE_END, "",
				DISCIPLINE_COL_ROW_COLOR, "#8EC22D",
				DISCIPLINE_COL_ROW_COLOR_SET, FALSE,
				-1 );
	    */
	}
	break;
    }
    case BUTTON_DISC_REM:
    {
	GtkTreeView *treeview_disciplines = NULL;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;

	treeview_disciplines = GTK_TREE_VIEW(
	    g_object_get_data( G_OBJECT( button ),
			       "treeview_disciplines" ) );
	gtk_tree_view_get_cursor( treeview_disciplines, &path, &column );

	/* Is there any row selected? */
	if( path != NULL )
	{
	    GtkWidget *store_disciplines = NULL;
	    GtkTreeIter iter;
	    gint depth;
	    
	    store_disciplines = g_object_get_data( G_OBJECT( button ),
				       "store_disciplines" );   
	    gtk_tree_model_get_iter( GTK_TREE_MODEL( store_disciplines ),
				     &iter, path );
	    depth = gtk_tree_store_iter_depth( GTK_TREE_STORE( store_disciplines ),
					       &iter );
	    if( depth == 0 )	    /* Is it the discipline a child? */
	    {
		gchar *cod = NULL;
		gchar *class = NULL;
		gint int_class = 0;
		
		semester *sem = g_object_get_data( G_OBJECT( button ), "semester" );
		
		gtk_tree_model_get( GTK_TREE_MODEL( store_disciplines ),
				    &iter,
				    DISCIPLINE_COD, &cod,
				    DISCIPLINE_CLASS, &class,
				    -1);
		sscanf( class, "%d", &int_class );
		if( semester_remove_discipline( sem, cod, int_class ) )
		{
		    GtkTreeModel *store_schedule = g_object_get_data(
			G_OBJECT( button ), "store_schedule" );
		    
		    gtk_tree_store_remove( GTK_TREE_STORE( store_disciplines ),
					   &iter );
		    tree_model_schedule_update( store_schedule, sem );
		}
		
	    } /* if */
	} /* if */
	break;
    } /* case */
    case BUTTON_DISC_ADD_SEED:
    {
	GtkTreeView *treeview_disciplines = NULL;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;

	treeview_disciplines = GTK_TREE_VIEW(
	    g_object_get_data( G_OBJECT( button ),
			       "treeview_disciplines" ) );
	gtk_tree_view_get_cursor( treeview_disciplines, &path, &column );
	
	if( path != NULL )
	{
	    GtkWidget *store_disciplines = NULL;
	    GtkTreeIter iter;
	    gint depth;
	    
	    store_disciplines = g_object_get_data( G_OBJECT( button ),
				       "store_disciplines" );   
	    gtk_tree_model_get_iter( GTK_TREE_MODEL( store_disciplines ),
				     &iter, path );
	    depth = gtk_tree_store_iter_depth( GTK_TREE_STORE( store_disciplines ),
					       &iter );
	    if( depth == 0 )
	    {
		GtkTreeIter iter2;
		GtkWidget *widget = NULL;
		seed *nseed = NULL;
		gchar cbegin[6];
		gint begin;
		gchar cend[6];
		gint end;
		gchar *cday;
		gint day;

		gchar *cod;
		gchar *class;
		gint int_class;
		discipline *disc;
		semester *sem;

		sem = g_object_get_data( G_OBJECT( button ),
					 "semester" );
		gtk_tree_model_get( GTK_TREE_MODEL( store_disciplines ),
				    &iter,
				    DISCIPLINE_COD, &cod,
				    DISCIPLINE_CLASS, &class,
				    -1);
		sscanf( class, "%d", &int_class );
		if( !(disc = semester_get_discipline( sem, cod, int_class ) ) )
		    return ;	/* None discipline finded */

		widget = g_object_get_data( G_OBJECT( button ),
					    "spinbutton_disc_begin_hour" );
		begin = gtk_spin_button_get_value_as_int(
		    GTK_SPIN_BUTTON( widget ) );
		begin = begin * 3600;
		    
		widget = g_object_get_data( G_OBJECT( button ),
					    "spinbutton_disc_begin_minute" );
		begin = begin +(60 * gtk_spin_button_get_value_as_int(
				    GTK_SPIN_BUTTON( widget ) ));

		widget = g_object_get_data( G_OBJECT( button ),
					    "spinbutton_disc_end_hour" );
		end = gtk_spin_button_get_value_as_int(
		    GTK_SPIN_BUTTON( widget ) );
		end = end * 3600;
		    
		widget = g_object_get_data( G_OBJECT( button ),
					    "spinbutton_disc_end_minute" );
		end = end +(60 * gtk_spin_button_get_value_as_int(
				GTK_SPIN_BUTTON( widget ) ));
		
		/* The end time is early than begin time... Strange... */
		if( end <= begin )
		    break;
		
		widget = g_object_get_data( G_OBJECT( button ),
					    "combobox_disc_day" );
		day = gtk_combo_box_get_active( GTK_COMBO_BOX( widget ) );
		cday = gtk_combo_box_get_active_text( GTK_COMBO_BOX( widget ) );

		nseed = seed_new( day, begin, end );
		if( discipline_add_seed( disc, nseed ) )
		{
		    time_toString( cbegin, begin );
		    time_toString( cend, end );

		    gtk_tree_store_append( GTK_TREE_STORE(store_disciplines), &iter2, &iter );
		    gtk_tree_store_set( GTK_TREE_STORE(store_disciplines), &iter2,
					DISCIPLINE_COD, "",
					DISCIPLINE_NAME, "",
					DISCIPLINE_CREDITS, "",
					DISCIPLINE_DAY, cday,
					DISCIPLINE_BEGIN, cbegin,
					DISCIPLINE_END, cend,
					-1);
		    gtk_tree_view_expand_to_path( treeview_disciplines, path);

		    GtkTreeModel *store_schedule = g_object_get_data(
			G_OBJECT( button ), "store_schedule" );
		    tree_model_schedule_update( store_schedule, sem );
		}
	    } 		
	}
	break;
    }
    case BUTTON_DISC_REM_SEED:
    {
	GtkTreeView *treeview_disciplines = NULL;
	GtkTreePath *path = NULL;
	GtkTreeViewColumn *column = NULL;

	treeview_disciplines = GTK_TREE_VIEW(
	    g_object_get_data( G_OBJECT( button ),
			       "treeview_disciplines" ) );
	gtk_tree_view_get_cursor( treeview_disciplines, &path, &column );

	/* Is there any row selected? */
	if( path != NULL )
	{
	    GtkWidget *store_disciplines = NULL;
	    GtkTreeIter iter;
	    gint depth;
	    
	    store_disciplines = g_object_get_data( G_OBJECT( button ),
				       "store_disciplines" );   
	    gtk_tree_model_get_iter( GTK_TREE_MODEL( store_disciplines ),
				     &iter, path );
	    depth = gtk_tree_store_iter_depth( GTK_TREE_STORE( store_disciplines ),
					       &iter );
	    /* It is a schedule */
	    if( depth == 1 )
	    {
		semester *sem = g_object_get_data( G_OBJECT( button ),
						   "semester" );
		discipline *disc = NULL;
		GtkTreeIter parent;
		gchar *day;
		gint int_day;
		gchar *begin;
		gint int_begin;
		gchar *end;
		gint int_end;
		gchar *cod;
		gchar *class;
		gint int_class;
		
		
		gtk_tree_model_get( GTK_TREE_MODEL( store_disciplines ),
				    &iter,
				    DISCIPLINE_DAY, &day,
				    DISCIPLINE_BEGIN, &begin,
				    DISCIPLINE_END, &end,
				    -1 );
		int_day = day_char_to_int( day );
		int_begin = time_toInteger( begin );
		int_end = time_toInteger( end );

		gtk_tree_model_iter_parent( GTK_TREE_MODEL( store_disciplines ),
					    &parent,
					    &iter );
		gtk_tree_model_get( GTK_TREE_MODEL( store_disciplines ),
				    &parent,
				    DISCIPLINE_COD, &cod,
				    DISCIPLINE_CLASS, &class,
				    -1 );
		sscanf( class, "%d", &int_class );
		disc = semester_get_discipline( sem, cod, int_class );
		
		if( discipline_remove_seed( disc, int_day, int_begin, int_end ) )
		{
		    GtkTreeModel *store_schedule = g_object_get_data(
			G_OBJECT( button ), "store_schedule" );
		    
		    gtk_tree_store_remove( GTK_TREE_STORE( store_disciplines ),
					   &iter );
		    tree_model_schedule_update( store_schedule, sem );
		}
		
	    } /* if */
	} /* if */
	break;
    } /* case */
    }
}
    
/*********************************************************************/

/* Events ************************************************************/
gboolean
std_event_handler( GtkWidget *widget,
		   GdkEvent  *event,
		   gpointer   data )
{
    gint handler = (gint) g_object_get_data( G_OBJECT(widget), "handler");

    switch( handler )
    {
    case DIALOG_ABOUT:
    {
	gtk_widget_hide( GTK_WIDGET( widget ) );
	return TRUE;
	break;
    }
    case DIALOG_FILECHOOSER_SAVE:
    {
	gtk_widget_hide( GTK_WIDGET( widget ) );
	return TRUE;
	break;
    }
    }
    
    return FALSE;
}

gboolean
quit_program( GtkWidget *widget,
	      GdkEvent  *event,
	      gpointer   data )
{
    gtk_main_quit ();
    return FALSE;
}
/*********************************************************************/

/* Functions related with the interface ******************************/

void
tree_model_schedule_update( GtkTreeModel *store_schedule, semester *sem )
{
    discipline *disc = NULL;
    int none = 0;
    
    gtk_tree_store_clear( GTK_TREE_STORE( store_schedule ) );

    while( !none )
    {
	disc = semester_get_next_discipline( sem, disc );
	if( disc )
	{
	    if( disc->scheded )
	    {
		tree_model_schedule_add_discipline( store_schedule,
						    disc );
	    }
	}
	else
	    none = 1;
    }
    
    return;
}

void
tree_model_schedule_add_discipline( GtkTreeModel *store_schedule, discipline *disc )
{
    GtkTreeIter iter_sche;
    seed *see = NULL;
    char begin[6];
    char end[6];
    int int_begin = 0;
    int int_end = 0;
    int gotcha = 0;
    	
    see = discipline_get_next_seed( disc, see );
    while( see )
    {
	if( gtk_tree_model_get_iter_first( store_schedule,
					   &iter_sche ) )
	{
	    gtk_tree_model_get( store_schedule,
				&iter_sche,
				SCHEDULE_BEGIN_INT, &int_begin,
				SCHEDULE_END_INT, &int_end,
				-1 );
	    if( (int_begin == see->begin) &&
		(int_end == see->end) )
	    {
		gotcha = 1;
	    } else {
		while( (gtk_tree_model_iter_next( store_schedule,
						  &iter_sche )) )
		{
		    gtk_tree_model_get( store_schedule,
					&iter_sche,
					SCHEDULE_BEGIN_INT, &int_begin,
					SCHEDULE_END_INT, &int_end,
					-1 );
		    if( (int_begin == see->begin) &&
			(int_end == see->end) )
		    {
			gotcha = 1;
			break;
		    }
		}
	    }
	}

	if( !gotcha )
	    gtk_tree_store_append( GTK_TREE_STORE(store_schedule),
				   &iter_sche, NULL );

	gotcha = 0;
	time_toString( begin, see->begin );
	time_toString( end, see->end );
			
	switch( see->day )
	{
	case SUNDAY:		/* Sunday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_SUNDAY, disc->cod,
				-1 );
	    break;
	}
	case MONDAY:		/* Monday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_MONDAY, disc->cod,
				-1 );
	    break;
	}
	case TUESDAY:		/* Tuesday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_TUESDAY, disc->cod,
				-1 );
	    break;
	}
	case WEDNESDAY:		/* Wednesday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_WEDNESDAY, disc->cod,
				-1 );
	    break;
	}
	case THURSDAY:		/* Thursday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_THURSDAY, disc->cod,
				-1 );
	    break;
	}
	case FRIDAY:		/* Friday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_FRIDAY, disc->cod,
				-1 );
	    break;
	}
	case SATURDAY:		/* Saturday */
	{
	    gtk_tree_store_set( GTK_TREE_STORE(store_schedule), &iter_sche,
				SCHEDULE_BEGIN_INT, see->begin,
				SCHEDULE_END_INT, see->end,
				SCHEDULE_BEGIN, begin,
				SCHEDULE_END, end,
				SCHEDULE_SATURDAY, disc->cod,
				-1 );
	    break;
	}
	}
	see = discipline_get_next_seed( disc, see );
    }
}

void
tree_model_disciplines_update( GtkTreeModel *store_disciplines, semester *sem )
{
    discipline *disc = NULL;
    int none = 0;
    
    gtk_tree_store_clear( GTK_TREE_STORE( store_disciplines ) );

    while( !none )
    {
	disc = semester_get_next_discipline( sem, disc );
	if( disc )
	    tree_model_disciplines_add_discipline( store_disciplines,
						   disc );
	else
	    none = 1;
    }    
    return;
}

void
tree_model_disciplines_add_discipline( GtkTreeModel *store_disciplines, discipline *disc )
{
    GtkTreeIter parent;
    GtkTreeIter child;
    seed *see = NULL;
    char class[3];
    char credits[3];
    
    snprintf( credits, 3, "%d", disc->credits );
    snprintf( class, 3, "%d", disc->class );

    gtk_tree_store_append( GTK_TREE_STORE(store_disciplines),
			   &parent, NULL );
    if( disc->scheded )
    {
	gtk_tree_store_set( GTK_TREE_STORE(store_disciplines),
			    &parent,
			    DISCIPLINE_COD, disc->cod,
			    DISCIPLINE_CLASS, class,
			    DISCIPLINE_NAME, disc->name,
			    DISCIPLINE_CREDITS, credits,
			    DISCIPLINE_DAY, "",
			    DISCIPLINE_BEGIN, "",
			    DISCIPLINE_END, "",
			    DISCIPLINE_COL_ROW_COLOR, "#8EC22D",
			    DISCIPLINE_COL_ROW_COLOR_SET, TRUE,
			    -1 );
    } else {
	gtk_tree_store_set( GTK_TREE_STORE(store_disciplines),
			    &parent,
			    DISCIPLINE_COD, disc->cod,
			    DISCIPLINE_CLASS, class,
			    DISCIPLINE_NAME, disc->name,
			    DISCIPLINE_CREDITS, credits,
			    DISCIPLINE_DAY, "",
			    DISCIPLINE_BEGIN, "",
			    DISCIPLINE_END, "",
			    DISCIPLINE_COL_ROW_COLOR, NULL,
			    DISCIPLINE_COL_ROW_COLOR_SET, FALSE,
			    -1 );

    }
    
    see = discipline_get_next_seed( disc, see );
    while( see )
    {
	char *cday;
	char cbegin[6];
	char cend[6];

	time_toString( cbegin, see->begin );
	time_toString( cend, see->end );
	cday = day_int_to_char( see->day );
	    
	gtk_tree_store_append( GTK_TREE_STORE(store_disciplines),
			       &child, &parent );
	gtk_tree_store_set( GTK_TREE_STORE(store_disciplines),
			    &child,
			    DISCIPLINE_COD, "",
			    DISCIPLINE_NAME, "",
			    DISCIPLINE_CREDITS, "",
			    DISCIPLINE_DAY, cday,
			    DISCIPLINE_BEGIN, cbegin,
			    DISCIPLINE_END, cend,
			    -1 );

	free(cday);
	see = discipline_get_next_seed( disc, see );
    }
}


/*********************************************************************/
